﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;
using Utils.ZPLPrinter;

namespace Printer
{
    /// <summary>
    /// 选择需要的打印机
    /// </summary>
    public partial class SelectPrinter : Form
    {
        public SelectPrinter()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 加载打印机数据界面
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Form1_Load(object sender, EventArgs e)
        {
            if (File.Exists(@"C:\MssClientCache\SelectPriter.txt"))
            {
                String PriterStr = File.ReadAllText(@"C:\MssClientCache\SelectPriter.txt");
                if (PriterStr != "")
                {
                    SelectedPrinterLabel.Text = PriterStr;
                    printerName.Text = "";
                }
            }
            else
                this.cmbInstalledPrinters.DroppedDown = true;

            foreach (string pkInstalledPrinters in System.Drawing.Printing.PrinterSettings.InstalledPrinters)
            {
                cmbInstalledPrinters.Items.Add(pkInstalledPrinters);
            }
        }

        /// <summary>
        /// 保存选择的打印机
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnPrintZPL_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(cmbInstalledPrinters.Text))
            {
                SelectedPrinterLabel.Text = cmbInstalledPrinters.Text;
                printerName.Text = "";
                String filepath = @"C:\MssClientCache\SelectPriter.txt";
                FileStream stream2 = File.Open(filepath, FileMode.OpenOrCreate, FileAccess.Write);
                stream2.Seek(0, SeekOrigin.Begin);
                stream2.SetLength(0); //清空txt文件
                stream2.Close();
                File.WriteAllText(filepath, cmbInstalledPrinters.Text);
            }
            this.Close();
        }
    }
}